/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.hpvas.gui;

import edu.hpvas.control.Login;
import java.awt.Color;
import java.awt.event.KeyEvent;
import javax.swing.JOptionPane;

/**
 *
 * @author raider
 */
public class FLogin extends javax.swing.JFrame {

    /**
     * Creates new form FLogin
     */
    public FLogin() {
        initComponents();
        this.setLocationRelativeTo(null);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        PLogin = new javax.swing.JPanel();
        LName = new javax.swing.JLabel();
        TFName = new javax.swing.JTextField();
        LLastName = new javax.swing.JLabel();
        TFLastName = new javax.swing.JTextField();
        LSystemName = new javax.swing.JLabel();
        BLogin = new javax.swing.JButton();
        LSystemDescription = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("HPVAS - Login");
        setResizable(false);

        PLogin.setBackground(new java.awt.Color(153, 153, 255));
        PLogin.setLayout(new java.awt.GridLayout(2, 2));

        LName.setLabelFor(TFName);
        LName.setText("Name");
        PLogin.add(LName);

        TFName.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TFNameMouseClicked(evt);
            }
        });
        TFName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFNameActionPerformed(evt);
            }
        });
        TFName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFNameKeyPressed(evt);
            }
        });
        PLogin.add(TFName);

        LLastName.setText("Last name");
        PLogin.add(LLastName);

        TFLastName.setMinimumSize(new java.awt.Dimension(6, 25));
        TFLastName.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                TFLastNameMouseClicked(evt);
            }
        });
        TFLastName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TFLastNameActionPerformed(evt);
            }
        });
        TFLastName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                TFLastNameKeyPressed(evt);
            }
        });
        PLogin.add(TFLastName);

        LSystemName.setFont(new java.awt.Font("Dialog", 1, 24)); // NOI18N
        LSystemName.setText("HPVAS");

        BLogin.setText("Login");
        BLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BLoginActionPerformed(evt);
            }
        });

        LSystemDescription.setFont(new java.awt.Font("Dialog", 2, 12)); // NOI18N
        LSystemDescription.setText("The Healthy Pets veterinarian  Association");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(LSystemDescription))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(PLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 320, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(BLogin))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(LSystemName)))
                .addContainerGap(51, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(LSystemName, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(LSystemDescription)
                .addGap(18, 18, 18)
                .addComponent(PLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(BLogin)
                .addContainerGap(36, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void BLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BLoginActionPerformed
        login();
    }//GEN-LAST:event_BLoginActionPerformed

    private void TFNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFNameActionPerformed
        
    }//GEN-LAST:event_TFNameActionPerformed

    private void login(){
        Login login=new Login();
        
        if(!TFName.getText().equals("")&&!TFLastName.getText().equals("")&&login.authenticate(TFName.getText(), TFLastName.getText())){ 
            MainFrame main=new MainFrame();
            main.setVisible(true);
            this.dispose();
        }else{
            if(TFName.getText().equals("") || TFLastName.getText().equals("")
                    || TFName.getText().equals("Person Name cannot be blank")
                    || TFLastName.getText().equals("Person LastName cannot be blank")){
                if(TFName.getText().equals("")){
                    TFName.setText("Person Name cannot be blank");
                    TFName.setForeground(Color.red);
                }
                if(TFLastName.getText().equals("")){
                    TFLastName.setText("Person LastName cannot be blank");
                    TFLastName.setForeground(Color.red);
                }
            }
            else{
                JOptionPane.showMessageDialog(this, "Sorry... Name or Last name are incorrect",
                        "Error",JOptionPane.ERROR_MESSAGE);
            }
        }
    }
    private void TFNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFNameKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            login();
        }
    }//GEN-LAST:event_TFNameKeyPressed

    private void TFNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TFNameMouseClicked
            if(TFName.getText().equals("Person Name cannot be blank")){
                TFName.setText("");
                TFName.setForeground(Color.black);
            }
    }//GEN-LAST:event_TFNameMouseClicked

    private void TFLastNameMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_TFLastNameMouseClicked
            if(TFLastName.getText().equals("Person LastName cannot be blank")){
                TFLastName.setText("");
                TFLastName.setForeground(Color.black);
            }
    }//GEN-LAST:event_TFLastNameMouseClicked

    private void TFLastNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TFLastNameActionPerformed
        
    }//GEN-LAST:event_TFLastNameActionPerformed

    private void TFLastNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_TFLastNameKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            login();
        }
    }//GEN-LAST:event_TFLastNameKeyPressed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FLogin.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FLogin().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BLogin;
    private javax.swing.JLabel LLastName;
    private javax.swing.JLabel LName;
    private javax.swing.JLabel LSystemDescription;
    private javax.swing.JLabel LSystemName;
    private javax.swing.JPanel PLogin;
    private javax.swing.JTextField TFLastName;
    private javax.swing.JTextField TFName;
    // End of variables declaration//GEN-END:variables
}
